package repositories;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.ArrayList;

import specifications.AirportSpecification;
import dbHelper.DBHelper;
import entities.Airport;

public class AirportRepository implements Repository<Airport> {
	
	public ArrayList<Airport> AirportList;
	
	
	public AirportRepository()
	{
		this.AirportList = null;
	}

	@Override
	public void Add(Airport Airport) {
		try {
			DBHelper.addObject(Airport);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void Delete(Airport Airport) {

		try {
			DBHelper.deleteObject(Airport);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | NoSuchMethodException
				| SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void Update(Airport Airport) {
		try {
			DBHelper.updateObject(Airport);
		} catch (NoSuchMethodException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException
				| SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	@Override
	public void FindAll() {
		Airport A = new Airport();
		try {
			this.AirportList = (ArrayList<Airport>) DBHelper.findObjects(A);
		} catch (IllegalAccessException | IllegalArgumentException
				| InvocationTargetException | InstantiationException
				| NoSuchMethodException | SecurityException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public ArrayList<Airport> findBySpecification (AirportSpecification spec) {
		if(this.AirportList == null)
			this.FindAll();
		ArrayList<Airport> res = new ArrayList<Airport>();
		for (Airport el : AirportList)
		{
			if (spec.isSatisfiedBy(el))
			{
				res.add(el);
			}
		}
		return res;
	}

}
